package it.storm.solution;

import java.util.ArrayList;
import java.util.List;

/**
 * 1827. 最少操作使数组递增
 * https://leetcode-cn.com/problems/minimum-operations-to-make-the-array-increasing/
 */
public class Solutions_1827 {
    public static void main(String[] args) {
        int[] nums1 = {1, 1, 1};  // output: 3
        int[] nums2 = {1, 5, 2, 4, 1};  // output: 14
        int[] nums3 = {8};  // output: 0

        List<int[]> inputs = new ArrayList<>();
        inputs.add(nums1);
        inputs.add(nums2);
        inputs.add(nums3);

        for (int[] input : inputs) {
            int result = minOperations(input);
            System.out.println(result);
        }
    }

    public static int minOperations(int[] nums) {
        int res = 0;
        // 临时变量记录前一位元素
        int last = nums[0];

        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > last) {
                last = nums[i];
                continue;
            }
            // 使 nums[i] 严格大于 nums[i - 1]
            res += last - nums[i] + 1;
            // 当前最少比前一位多 1
            last = last + 1;
        }
        return res;
    }
}
